package com.info33.platform.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.info33.platform.common.mvc.vo.Result;
import com.info33.platform.system.dao.SysShortcutMenuDao;
import com.info33.platform.system.dao.SysTenantMenuDao;
import com.info33.platform.system.entity.SysShortcutMenu;
import com.info33.platform.system.entity.SysTenantMenu;
import com.info33.platform.system.service.SysShortcutMenuService;
import com.info33.platform.system.vo.SysShortcutMenuListVO;
import com.info33.platform.system.vo.SysShortcutMenuVO;
import com.info33.platform.system.vo.SysUserVO;

/**
 * @author admin
 */
@Service
public class SysShortcutMenuServiceImpl extends ServiceImpl<SysShortcutMenuDao, SysShortcutMenu> implements SysShortcutMenuService {

    @Autowired
    private SysShortcutMenuDao sysShortcutMenuDao;
    @Autowired
    private SysTenantMenuDao sysTenantMenuDao;

	@Override
	public Page<SysShortcutMenuVO> pageShortcutMenu(Page<SysShortcutMenuVO> page, SysUserVO sysUserVO) {
		return sysShortcutMenuDao.pageShortcutMenu(page, sysUserVO);
	}

	@Override
	public Result<List<SysShortcutMenu>> saveBatchShortcutMenu(List<SysShortcutMenu> sysShortcutMenuList,
			SysUserVO sysCurrentUser) {
		if(CollectionUtils.isEmpty(sysShortcutMenuList)) {
			return Result.error("请选择菜单");
		}
		List<Long> shortcutMenuIds = new ArrayList<Long>();
		for(SysShortcutMenu sysShortcutMenu : sysShortcutMenuList) {
			SysShortcutMenu shortcutMenu = sysShortcutMenuDao.selectOne(new QueryWrapper<SysShortcutMenu>()
					.eq("menu_id", sysShortcutMenu.getMenuId())
					.eq("create_by", sysCurrentUser.getSysTenantUserVO().getId()));
			if(shortcutMenu != null) {
				shortcutMenuIds.add(shortcutMenu.getId());
			}
			SysTenantMenu sysTenantMenu = sysTenantMenuDao.selectById(sysShortcutMenu.getMenuId());
			sysShortcutMenu.setQuickBg(sysTenantMenu.getQuickBg());	
		}
		if(!CollectionUtils.isEmpty(shortcutMenuIds)) {
			sysShortcutMenuDao.deleteBatchIds(shortcutMenuIds);
		}
		
		return saveBatch(sysShortcutMenuList) ? Result.ok(sysShortcutMenuList) : Result.error();
	}

	@Override
	public List<SysShortcutMenuListVO> listShortcutMenu(SysUserVO sysCurrentUser) {
		return sysShortcutMenuDao.listShortcutMenu(sysCurrentUser);
	}

}